<template>
  <!-- 条件栏 -->
  <NewPageFilter
    v-model:query="filterInfo.query.queryMap"
    :filter-list="filterInfo.searchList"
    :filter-btn="filterInfo.searchBtn"
    :list-type-info="listTypeInfo"
  />
  <DialogTable
    className="reprotTable"
    :refresh="tableInfo.refresh"
    :init-curpage="tableInfo.initCurpage"
    v-model:data="tableInfo.data"
    :api="api"
    ref="resourceTable"
    :query="filterInfo.query.queryMap"
    :field-list="tableInfo.fieldList"
    :handle="tableInfo.handle"
    :highlight-row="highlightRow"
    :checked-list="checkedList"
    :reserve-selection="true"
    :check-box="checkBox"
    @handleEvent="handleEvent"
  >
    <template #col-commonName="scope">
      <p>{{ scope.row.commonName || '-' }}</p>
      <p>{{ scope.row.productCode || '-' }}</p>
      <p>{{ scope.row.alias || '-' }}</p>
    </template>
    <template #col-standard="scope">
      <p>{{ scope.row.standard || '-' }}</p>
      <p>{{ scope.row.unitName || '-' }}</p>
    </template>
    <template #col-type="scope">
      <p>{{ scope.row.productTypeValue || '-' }}</p>
      <p>{{ scope.row.codeTypeValue || '-' }}</p>
      <p>{{ scope.row.productCategoryValue || '-' }}</p>
    </template>
    <template #col-supplierName="scope">
      <p>{{ scope.row.supplierName || '-' }}</p>
    </template>
    <template #col-licenseNumber="scope">
      <p>{{ scope.row.licenseNumber || '-' }}</p>
      <p>{{ scope.row.brandName || '-' }}</p>
    </template>
  </DialogTable>
</template>
<script setup name="selectProductReport">
import { getProductPage, getSupplierAll } from '@/api/core.js'
const { proxy } = getCurrentInstance()

const props = defineProps({
  highlightRow: {
    type: Boolean,
    default: false,
  },
  checkBox: {
    type: Boolean,
    default: true,
  },
  api: {
    type: Function,
    default: getProductPage,
  },
  showSupplierName: {
    type: Boolean,
    default: false,
  },
  query: {
    type: Object,
    default: function () {
      return {}
    },
  },
  showStock: {
    type: Boolean,
    default: false,
  },
  checkedList: {
    type: Array,
    default: () => {
      return []
    },
  },
})

// 过滤相关配置
const filterInfo = ref({
  query: {
    sortString: '',
    groupMap: {},
    groups: [],
    queryMap: {
      searchType: 'product',
      status: 1,
      type: 0,
      sort: 'common_name,standard',
      ...props.query,
    },
  },
  searchList: [
    {
      componentType: 'input',
      label: '名称/别名/编码/助记符',
      value: 'keyword',
      event: search,
      hidden: false,
      show: true,
    },
    {
      label: '供应商',
      value: 'supplierIdMult',
      componentType: 'slip-select',
      api: getSupplierAll,
      query: { sort: 'create_time desc' },
      clearable: true,
      multiple: true,
      keyField: 'supplierName',
      valueField: 'supplierId',
      event: search,
    },
  ],
  searchBtn: [{ type: 'button', label: '搜索', event: search }],
})
// 表格相关
const tableInfo = ref({
  refresh: 1,
  initCurpage: 1,
  data: [],
  fieldList: [
    {
      label: '通用名称\n编码\n别名',
      value: 'commonName',
      minWidth: 220,
      type: 'slot',
    },
    { label: '规格\n计量单位', value: 'standard', type: 'slot' },
    {
      label: '产品类型\n条码类型\n产品分类',
      value: 'type',
      minWidth: 150,
      type: 'slot',
    },
    { label: '助记符', value: 'mnemonic', width: '100px' },
    {
      label: '证号\n生产厂家',
      value: 'licenseNumber',
      minWidth: 220,
      type: 'slot',
    },
  ],
})
const selectRow = ref([])
const listTypeInfo = ref({
  supplierList: [],
})

watch(
  () => tableInfo.value.data,
  (val) => {
    if (val.length && props.checkedList.length) {
      props.checkedList.forEach((row) =>
        proxy.$refs['resourceTable'].toggleRowSelection(row, true)
      )
    }
    /* if (val.length) {
      window.parent.postMessage({ type: 'customDialogShow', data: {} }, '*')
    } */
  }
)
window.addEventListener('message', receiveMessageIframePage, false)
function receiveMessageIframePage(data) {
  let res = data.data
  // 重置选中
  if (res.type === 'customDialogReset') {
    tableInfo.value.initCurpage = Math.random()
    proxy.$refs.resourceTable.clearSelection()
  }
  /* if (res.type === 'customDialogSelect') {
    let arr = []
    let data = res.data
    if (data && data.length) {
      tableInfo.value.data.filter((element) => {
        data.forEach((item) => {
          if (element.id === item.value) {
            arr.push(element)
          }
        })
      })
      arr.forEach((element) => {
        proxy.$refs['resourceTable'].toggleRowSelection(element, true)
      })
    }
  } */
}
// 获取列表
function getList() {
  tableInfo.value.refresh = Math.random()
}
function search() {
  tableInfo.value.initCurpage = Math.random()
  tableInfo.value.refresh = Math.random()
}
function handleEvent(event, data) {
  switch (event) {
    case 'tableClickRow':
      handelClickRow(data)
      selectRow.value = data
      break
    case 'tableCheck':
      handelClickRow(data)
      selectRow.value = data
      break
  }
}
function handelClickRow(data) {
  window.parent.postMessage(
    { type: 'tableClickRow', data: handleCheckData(data) },
    '*'
  )
}
function handleCheckData(data) {
  return data.map((element) => {
    return {
      label: element.commonName,
      value: element.id,
    }
  })
}
onMounted(() => {
  // hack, 在table创建出来之后再去调用
  proxy.$nextTick(() => {
    if (props.showStock) {
      tableInfo.value.fieldList.push({
        label: '库存数量',
        value: 'stockQty',
        type: 'column',
        formatter: proxy.$fn.qtyFormatter,
      })
    }
    if (props.showSupplierName) {
      tableInfo.value.fieldList.splice(3, 0, {
        label: '供应商名称',
        value: 'supplierName',
        minWidth: 150,
        type: 'slot',
      })
    }
    getList()
  })
})
</script>
